<?php
	
	//add by lin
	
	define('__ROOT__', dirname(dirname(__FILE__)));
	require_once(__ROOT__.'\models\Conn.php'); 
	require_once(__ROOT__.'\models\Economic.php');
	//end add
	
class EconomicController extends Controller{

	private $id;

	public function base(){
		
	}

	public function showEconomicList(){
		
		$e1 = new Economic();		
		$economic_list = $e1->listAll();
		$arg_item = array();
		foreach( $economic_list as $item )
		{
			array_push($arg_item,implode("/",$item));
		}
		$arg = implode(":",$arg_item);
		header("Location:app/views/economic.php?list=$arg");
	}
	
	public function editPage()
	{
		$id = $this->id;
		//根据这个id值从数据库里获取相应的数据记录，这边暂时假设根据这个id获得到了数据，如下
		$e1 = new Economic();
		$item = $e1->getById($id);
		//var_dump($item);die;		
		//print_r($item);
		$arg = implode(":",$item);
		header("Location:app/views/edit_economic.php?item=$arg");
	}
	
	public function deleteItem()
	{
		//var_dump('delete');
		$id = $this->id;
		//根据这个id值从数据库里删除一条记录
		
		$e1 = new Economic();
		$e1->delete($id);
		
		$this->showEconomicList();
	}
	
	public function addItemPage()
	{
		header("Location:app/views/add_economic.php");
	}
	
	//没改
	public function addItem()
	{
		
		$new_item_name = $_POST['name'];
		$new_item_value = $_POST['value'];
		$new_item_unit = $_POST['unit'];
		$new_item_year = $_POST['year'];
		$new_item_month = $_POST['month'];
		//根据以上的值在数据库插入一条新记录
		$item = array("name"=>$new_item_name,"value"=>$new_item_value,"unit"=>$new_item_unit,"year"=>$new_item_year,"month"=>$new_item_month);
		//add by lin
		$e1 = new Economic();
		$e1->insert($item);
		$this->showEconomicList();
	}

	public function setParams($params)
	{
		$this->id = $params[0];
	}
}
